package com.wms.service.impl;

import com.wms.dto.AddStkByLocationDTO;
import com.wms.dto.AddStkLocationLocDTO;
import com.wms.entity.StkLocation;
import com.wms.entity.StkWarehouse;
import com.wms.mapper.AddStkByLocationMapper;
import com.wms.service.AddStkByLocationService;
import com.wms.utils.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class AddStkByLocationServiceImpl implements AddStkByLocationService {
    @Autowired
    AddStkByLocationMapper addStkByLocationMapper;

    @Override
    @Transactional
    public int AddStkByLocation(AddStkByLocationDTO addStkByLocationDTO) {
        //1.生成盘点任务号
        String maxCode = addStkByLocationMapper.getMaxCode();
        String newCode = null;
        if (maxCode != null && maxCode.startsWith("PD")) {
            String numberPart = maxCode.substring(2);
            int num = Integer.parseInt(numberPart) + 1;
            newCode = "PD" + String.format("%07d", num);
        }
        addStkByLocationDTO.setCode(newCode);
        //设置创建人  先写死
        addStkByLocationDTO.setUserId(1);
        //2.保存盘点任务
        int result = addStkByLocationMapper.AddStkByLocation(addStkByLocationDTO);
        Assert.isZero(result, "盘点任务创建失败");

        //3.遍历库区
        if (null != addStkByLocationDTO.getAreaList() && addStkByLocationDTO.getAreaList().size() > 0) {
            //遍历
            addStkByLocationDTO.getAreaList().forEach(el -> {
                int tmp = 0;
                //每次遍历是一个库区  统计该库区下盘点任务量
                int taskNum = 0;//盘点任务量

                //遍历该库区下 需要盘点的货位
                for (int i = 0, len = el.getStkLocationLocDTOList().size(); i < len; i++) {
                    //获取当前货位信息
                    AddStkLocationLocDTO addStkLocationLocDTO = el.getStkLocationLocDTOList().get(i);
                    //每遍历一个货位 统计该货位下盘点数量
                    //查询该货位下 所有可用数量大于0的物料 这些物料将会作为任务明细
                    tmp = addStkByLocationMapper.saveStkDetail(addStkByLocationDTO.getId(), addStkLocationLocDTO.getId());
                    Assert.isZero(tmp, "盘点任务明细保存失败");
                    //根据货位统计 需要盘点的任务数量
                    int locationNum = addStkByLocationMapper.queryLocCount(addStkLocationLocDTO.getId());
                    //保存该货位的统计信息
                    StkLocation stkLocation = new StkLocation();
                    stkLocation.setTaskId(addStkByLocationDTO.getId());
                    stkLocation.setLocationId(addStkLocationLocDTO.getId());
                    stkLocation.setLocation(addStkLocationLocDTO.getName());
                    stkLocation.setTaskNum(locationNum);
                    stkLocation.setAreaId(el.getId());
                    stkLocation.setFinishedNum(0);
                    stkLocation.setUnfinishedNum(locationNum); // 设置未完成数量
                    int result2 = addStkByLocationMapper.saveStkLocation(stkLocation);
                    Assert.isZero(result2, "盘点任务货位统计信息保存失败");
                    //库区数量累加
                    taskNum += locationNum;
                }
                //保存该库区的统计信息
                StkWarehouse stkWarehouse = new StkWarehouse();
                stkWarehouse.setTaskId(addStkByLocationDTO.getId());
                stkWarehouse.setAreaId(el.getId());
                stkWarehouse.setArea(el.getName());
                stkWarehouse.setTaskNum(taskNum);
                stkWarehouse.setFinishedNum(0);
                stkWarehouse.setUnfinishedNum(taskNum); // 设置未完成数量
                int result3 = addStkByLocationMapper.saveStkWarehouse(stkWarehouse);
                Assert.isZero(result3, "盘点任务库区统计信息保存失败");
            });
        }
        return 1;
    }
}
